import Vuex from 'vuex'
import Vue from 'vue'
import {getToken, setToken, removeToken} from '@/request/token'
import {login, getUserInfo, logout, register} from '@/api/login'
import { getId, getUsername, removeId, removeUsername, setId, setUsername } from '../request/token';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    id: getId(),
    username: getUsername(),
    token: getToken(),
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_USERNAME: (state, username) => {
      state.username = username
    },
    SET_ID: (state, id) => {
      state.id = id
    } ,
  },
  actions: {
    // 登录
    loginAction({commit}, user) {
      return new Promise((resolve, reject) => {
        login(user.username, user.password , user.uuid , user.captcha)
        .then(data => {
          // console.log("loginAction data : " , data)
          if( data.success ){
            commit('SET_TOKEN', data.data)
            commit('SET_USERNAME' , user.username)
            commit('SET_ID' , data.id)
            setToken(data.data)
            setUsername(user.username)
            setId(data.id)
            resolve()
          }else{
            reject(data.msg)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    getUserInfo({commit, state}) {
      return new Promise((resolve, reject) => {
        getUserInfo(state.token)
        .then(data => {
          if ( data.success ) {
            commit('SET_USERNAME', data.data.userName)
            commit('SET_ID', data.data.id)
            resolve(data)
          } else {
            commit('SET_USERNAME', '')
            commit('SET_ID', '')
            removeToken()
            removeUsername()
            removeId()
            resolve(data)
          }

        }).catch(error => {
          commit('SET_USERNAME', '')
          commit('SET_ID', '')
          removeToken()
          removeUsername()
          removeId()
          reject(error)
        })
      })
    },

    // 登出
    logoutAction({commit, state}) {
      return new Promise((resolve, reject) => {
        logout(state.token)
        .then(data => {
          if( data.code === 200 ){
            commit('SET_TOKEN', '')
            commit('SET_USERNAME', '')
            commit('SET_ID', '')
            removeToken()
            removeUsername()
            removeId()
            resolve()
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    fedLogOut({commit}) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_USERNAME', '')
        commit('SET_ID', '')
        removeToken()
        removeUsername()
        removeId()
        resolve()
      }).catch(error => {
        reject(error)
      })
    },

    // 注册
    registerAction({commit}, user) {
      return new Promise((resolve, reject) => {
        register(
            user.username ,
            user.nickname ,
            user.password ,
            user.email ,
            user.mobile ,
            user.status ,
          ).then((data) => {
            if(data.success){
              commit('SET_TOKEN', data.data)
              commit('SET_USERNAME' , user.username)
              commit('SET_ID' , data.id)
              setToken(data.data)
              setUsername(user.username)
              setId(data.id)
              resolve()
          }else{
            reject(data.msg)
          }
        }).catch((error) => {
          reject(error)
        })
      })
    } ,
  }
})
